package com.admin.controller;


import cn.hutool.core.io.FileUtil;
import com.admin.entity.Admin;
import com.admin.entity.Apk;
import com.admin.service.IAdminService;
import com.admin.service.IApkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.token.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/apk")
public class ApkController {


    @Autowired
    private IApkService apkService;

    @Value("${upload.tmp.dir}")
    private String tmp;

    @Value("${upload.dir}")
    private String upload;

    @Autowired
    private  IAdminService adminService;


    @Autowired
    HttpServletRequest request;

    /**
     * 查询:统计普通apk被访问次数（柱状统计图）
     * @return
     */
    @GetMapping
    @RequestMapping(value = "/searchApkWithVisit",method = RequestMethod.GET)
    public ResponseBean searchApkWithVisit(){
    List list = apkService.list();
    if (list!=null)
    {
        return new ResponseBean(200,"操作成功",list);
    }
    return new ResponseBean(500,"操作失败",list);
    }

    /**
     * 查询普通Apk 以及根据条件查询
     * @param page
     * @param limit
     * @param
     * @param map
     * @return
     */


    @RequestMapping("/searchApkWithPageById")
    public ResponseBean searchApkWithPageById(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit,String token,@RequestParam Map map)
    {
        String username = JwtUtil.getUsername(token);
        Admin admin = adminService.getAdminByUserName(username);
        int adminId = admin.getAdminId();
        IPage result = apkService.searchApkWithPageById(page,limit,adminId,map);
        return new ResponseBean(0,result.getTotal(),result.getRecords());
    }

    /**
     *新增普通apk应用
     * @param apk
     * @return
     */
    @RequestMapping(value = "/addApk",method = RequestMethod.POST)
    public ResponseBean addApk(Apk apk)
    {

        String token = request.getHeader("token");
        String username = JwtUtil.getUsername(token);
        //截取得到文件名
        String oldPath = apk.getApkUrl();
        String newPath = oldPath.substring(oldPath.lastIndexOf("\\")+1);
        int insert = apkService.addApk(apk,username);
        if (insert>0)
        {
            //将图片从临时目录复制到正式目录
            FileUtil.copy(tmp+"/"+apk.getApkPictureUrl(),upload+"/"+apk.getApkPictureUrl(),true);
            //将apk应用文件从临时目录复制到正式目录
            FileUtil.copy(tmp+"/"+newPath,upload+"/"+newPath,true);
            return new ResponseBean(200,"新增成功","");
        }
        return new ResponseBean(500,"新增失败","");
    }


    /**
     * 是否有权限编辑
     * @param adminTr
     * @param token
     * @return
     */
    @RequestMapping(value = "/isItQuanxian",method = RequestMethod.POST)
    public ResponseBean isItQuanxian(int adminTr,String token){
        String username = JwtUtil.getUsername(token);
        if(apkService.findAdminByPhone(username)==null){
            return new ResponseBean(500,null,"没有权限");
        }
        return new ResponseBean(200,null,null);
    }



    /**
     * 根据主键修改普通apk
     * @param apk
     * @return
     */
    @RequestMapping(value = "/updateApkByApkId",method = RequestMethod.POST)
    public ResponseBean updateApkByApkId(Apk apk)
    {
        String username = JwtUtil.getUsername(request.getHeader("token"));
        Admin admin = apkService.findAdminByPhone(username);
        //MD5的比较判断传入apk是否重复
        if(apkService.selectApkMd5AndApkAdmin(apk.getApkMd5(),admin.getAdminId())!=null){
            return new ResponseBean(500,null,"文件已上传");
        }

        if(apk == null)
        {
            return new ResponseBean(0,null,null);
        }
        //截取得到文件名
        String oldPath = apk.getApkUrl();
        String newPath = oldPath.substring(oldPath.lastIndexOf("\\")+1);
        boolean f = apkService.updateById(apk);
        if(f)
        {
            //将文件从临时目录复制到正式目录
            FileUtil.copy(tmp+"/"+apk.getApkPictureUrl(),upload+"/"+apk.getApkPictureUrl(),true);
            //将apk应用文件从临时目录复制到正式目录
            FileUtil.copy(tmp+"/"+newPath,upload+"/"+newPath,true);
            return new ResponseBean(200,"编辑成功",null);
        }
        return new ResponseBean(500,"编辑失败",null);
    }


    /**
     * 根据主键修改是否公开
     * @param map
     * @return
     */
    @PostMapping("/updateApkByApkIdWithPublic")
    public ResponseBean updateApkByApkIdWithPublic(@RequestParam Map map)
    {
        if(map == null)
        {
            return new ResponseBean(0,null,null);
        }
        Apk apk = apkService.getById((String)map.get("data[apk_id]"));
        if("公开".equals(apk.getApkIsItPublic())){
            apk.setApkIsItPublic("不公开");
        }else{
            apk.setApkIsItPublic("公开");
        }
        boolean f = apkService.updateById(apk);
        int code = f ? 200 : 500;
        return new ResponseBean(code,null,apk);
    }


    /**
     * 根据主键删除普通apk
     * @param apkId
     * @return
     */
    @RequestMapping(value = "/deleteApkByApkId",method = RequestMethod.POST)
    public ResponseBean deleteApkByApkId(int apkId)
    {
        boolean f = apkService.removeById(apkId);
        int code = f ? 200 : 500;
        return new ResponseBean(code,null,null);
    }


    /**修改apk的状态
     * @param apk
     * @return
     */
    @RequestMapping("/updataApkShenhe")
    public ResponseBean updataApkShenhe(Apk apk){
        apk.setApkShenhe("已审核");
        boolean f=apkService.updateById(apk);
        int code=f?200:500;
        return  new ResponseBean(code,null,null);
    }

    /**
     * 查所有未审核的apk
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selApkShehe")
    public ResponseBean selApkShenhe(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        IPage iPage = apkService.apkShenhe(page, limit);
        return new ResponseBean(0, iPage.getTotal(), iPage.getRecords());
    }





}
